package com.aierkeji.call.cms.service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

import javax.transaction.Transactional;
import javax.transaction.Transactional.TxType;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.aierkeji.call.cms.dao.jpa.AkjCallReportDao;
import com.aierkeji.call.cms.entity.AkjCallClueGroup;
import com.aierkeji.call.cms.entity.AkjCallClueStatistics;
import com.aierkeji.call.cms.entity.AkjCallUser;
import com.aierkeji.call.cms.util.DateTimeUtils;

@Service
public class AsyncServices {
	@Autowired
	AkjCallReportDao akjCallReportDao;
	private final Logger log = LogManager.getLogger(getClass());

	@Async("asyncServiceExecutor")
//	@Transactional(value=TxType.REQUIRES_NEW)
	public void createStatic(AkjCallUser akjCallUser, AkjCallClueGroup akjCallClueGroup, int count) {
		log.info("in createStatic, akjCallUser:{},akjCallClueGroup:{},count:{}", akjCallUser.getId(),
				akjCallClueGroup.getId(), count);
		AkjCallClueStatistics akjCallClueStatistics = new AkjCallClueStatistics();
		akjCallClueStatistics.setCallUserId(akjCallUser.getId());
		akjCallClueStatistics.setGroupId(akjCallClueGroup.getId());
		LocalDateTime localDate = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
		int year = localDate.getYear();
		int month = localDate.getMonthValue();
		int day = localDate.getDayOfMonth();
		akjCallClueStatistics.setYear((long) year);
		akjCallClueStatistics.setMonth((long) month);
		akjCallClueStatistics.setDay((long) day);
		akjCallClueStatistics.setCreatedAt(java.util.Date.from(Instant.now()));
		akjCallClueStatistics.setClueTotal((long) count);
		akjCallClueStatistics.setStatisticsCreatedAt(DateTimeUtils.getCurrentTime());
		count = akjCallReportDao.countBy(akjCallClueStatistics);
		if (count <= 0) {
			akjCallReportDao.save(akjCallClueStatistics);
			log.info("save createStatic, akjCallUser:{},akjCallClueGroup:{},count:{}", akjCallUser.getId(),
					akjCallClueGroup.getId(), count);
		}
		log.info("end createStatic, akjCallUser:{},akjCallClueGroup:{},count:{}", akjCallUser.getId(),
				akjCallClueGroup.getId(), count);
	}

	@Async("asyncServiceExecutor")
//	@Transactional(value=TxType.REQUIRES_NEW)
	public void createStatic(List<CreateStatic> createStatics) {
		createStatics.forEach(k -> {
			AkjCallUser akjCallUser = k.akjCallUser;
			AkjCallClueGroup akjCallClueGroup = k.akjCallClueGroup;
			int count = k.count;

			log.info("in createStatic, akjCallUser:{},akjCallClueGroup:{},count:{}", akjCallUser.getId(),
					akjCallClueGroup.getId(), count);
			AkjCallClueStatistics akjCallClueStatistics = new AkjCallClueStatistics();
			akjCallClueStatistics.setCallUserId(akjCallUser.getId());
			akjCallClueStatistics.setGroupId(akjCallClueGroup.getId());
			LocalDateTime localDate = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
			int year = localDate.getYear();
			int month = localDate.getMonthValue();
			int day = localDate.getDayOfMonth();
			akjCallClueStatistics.setYear((long) year);
			akjCallClueStatistics.setMonth((long) month);
			akjCallClueStatistics.setDay((long) day);
			akjCallClueStatistics.setCreatedAt(java.util.Date.from(Instant.now()));
			akjCallClueStatistics.setClueTotal((long) count);
			akjCallClueStatistics.setStatisticsCreatedAt(DateTimeUtils.getCurrentTime());
			count = akjCallReportDao.countBy(akjCallClueStatistics);
			if (count <= 0) {
				akjCallReportDao.save(akjCallClueStatistics);
				log.info("save createStatic, akjCallUser:{},akjCallClueGroup:{},count:{}", akjCallUser.getId(),
						akjCallClueGroup.getId(), count);
			}
			log.info("end createStatic, akjCallUser:{},akjCallClueGroup:{},count:{}", akjCallUser.getId(),
					akjCallClueGroup.getId(), count);
		});
	}

	public static class CreateStatic {
		public AkjCallUser akjCallUser;
		public AkjCallClueGroup akjCallClueGroup;
		public int count;

		public static CreateStatic build(AkjCallUser akjCallUser, AkjCallClueGroup akjCallClueGroup, int count) {
			CreateStatic createStatic = new CreateStatic();
			createStatic.akjCallClueGroup = akjCallClueGroup;
			createStatic.akjCallUser = akjCallUser;
			createStatic.count = count;
			return createStatic;
		}

	}
}
